#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ScholarInfoProvider : CY.GFive.Core.Providers.DALProvider.IScholarInfoProvider
	{
		#region Sql
		private static readonly string SqlSelectScholarInfo = "SELECT [Id], [StdCode],[ClsCode],[Grade],[YearNum],[Term],[Degree],[AssDate],[AssPeerCode],[AssPeerName],[SchlCate] FROM [ScholarInfo] WHERE  [Id]=@Id";

		private static readonly string SqlInsertScholarInfo = "INSERT INTO [ScholarInfo]([StdCode],[ClsCode],[Grade],[YearNum],[Term],[Degree],[AssDate],[AssPeerCode],[AssPeerName],[SchlCate]) values(@StdCode,@ClsCode,@Grade,@YearNum,@Term,@Degree,@AssDate,@AssPeerCode,@AssPeerName,@SchlCate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateScholarInfo = "UPDATE [ScholarInfo] SET [StdCode] = @StdCode,[ClsCode] = @ClsCode,[Grade] = @Grade,[YearNum] = @YearNum,[Term] = @Term,[Degree] = @Degree,[AssDate] = @AssDate,[AssPeerCode] = @AssPeerCode,[AssPeerName] = @AssPeerName,[SchlCate] = @SchlCate WHERE  [Id]=@Id";

		private static readonly string SqlDeleteScholarInfo = "DELETE [ScholarInfo] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllScholarInfo = "SELECT [Id], [StdCode],[ClsCode],[Grade],[YearNum],[Term],[Degree],[AssDate],[AssPeerCode],[AssPeerName],[SchlCate] FROM [ScholarInfo]";
		#endregion

		#region IScholarInfoProvider Members
		public Core.Business.ScholarInfo Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectScholarInfo);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ScholarInfo scholarInfo = new Core.Business.ScholarInfo();

				if (!reader.IsDBNull(0)) scholarInfo.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) scholarInfo.StdCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) scholarInfo.ClsCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) scholarInfo.Grade = reader.GetString(3);
				if (!reader.IsDBNull(4)) scholarInfo.YearNum = reader.GetString(4);
				if (!reader.IsDBNull(5)) scholarInfo.Term = reader.GetInt32(5);
				if (!reader.IsDBNull(6)) scholarInfo.Degree = reader.GetString(6);
				if (!reader.IsDBNull(7)) scholarInfo.AssDate = reader.GetDateTime(7);
				if (!reader.IsDBNull(8)) scholarInfo.AssPeerCode = reader.GetString(8);
				if (!reader.IsDBNull(9)) scholarInfo.AssPeerName = reader.GetString(9);
				if (!reader.IsDBNull(10)) scholarInfo.SchlCate = reader.GetString(10);

				return scholarInfo;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ScholarInfo scholarInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, scholarInfo.StdCode);
			sql.AddParameter("@ClsCode", SqlDbType.NVarChar, scholarInfo.ClsCode);
			sql.AddParameter("@Grade", SqlDbType.NVarChar, scholarInfo.Grade);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, scholarInfo.YearNum);
			sql.AddParameter("@Term", SqlDbType.Int, scholarInfo.Term);
			sql.AddParameter("@Degree", SqlDbType.NVarChar, scholarInfo.Degree);
			sql.AddParameter("@AssDate", SqlDbType.DateTime, scholarInfo.AssDate);
			sql.AddParameter("@AssPeerCode", SqlDbType.NVarChar, scholarInfo.AssPeerCode);
			sql.AddParameter("@AssPeerName", SqlDbType.NVarChar, scholarInfo.AssPeerName);
			sql.AddParameter("@SchlCate", SqlDbType.NVarChar, scholarInfo.SchlCate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertScholarInfo);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) scholarInfo.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ScholarInfo scholarInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, scholarInfo.StdCode);
			sql.AddParameter("@ClsCode", SqlDbType.NVarChar, scholarInfo.ClsCode);
			sql.AddParameter("@Grade", SqlDbType.NVarChar, scholarInfo.Grade);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, scholarInfo.YearNum);
			sql.AddParameter("@Term", SqlDbType.Int, scholarInfo.Term);
			sql.AddParameter("@Degree", SqlDbType.NVarChar, scholarInfo.Degree);
			sql.AddParameter("@AssDate", SqlDbType.DateTime, scholarInfo.AssDate);
			sql.AddParameter("@AssPeerCode", SqlDbType.NVarChar, scholarInfo.AssPeerCode);
			sql.AddParameter("@AssPeerName", SqlDbType.NVarChar, scholarInfo.AssPeerName);
			sql.AddParameter("@SchlCate", SqlDbType.NVarChar, scholarInfo.SchlCate);
			sql.AddParameter("@Id", SqlDbType.Int, scholarInfo.Id);
			sql.ExecuteSql(SqlUpdateScholarInfo);
		}

		public void Delete(Core.Business.ScholarInfo scholarInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, scholarInfo.Id);
			sql.ExecuteSql(SqlDeleteScholarInfo);
		}

		public IList<Core.Business.ScholarInfo> GetAllScholarInfo()
		{
			IList<Core.Business.ScholarInfo> scholarInfolist = new List<Core.Business.ScholarInfo>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllScholarInfo);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ScholarInfo scholarInfo = new Core.Business.ScholarInfo();

					if (!reader.IsDBNull(0)) scholarInfo.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) scholarInfo.StdCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) scholarInfo.ClsCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) scholarInfo.Grade = reader.GetString(3);
					if (!reader.IsDBNull(4)) scholarInfo.YearNum = reader.GetString(4);
					if (!reader.IsDBNull(5)) scholarInfo.Term = reader.GetInt32(5);
					if (!reader.IsDBNull(6)) scholarInfo.Degree = reader.GetString(6);
					if (!reader.IsDBNull(7)) scholarInfo.AssDate = reader.GetDateTime(7);
					if (!reader.IsDBNull(8)) scholarInfo.AssPeerCode = reader.GetString(8);
					if (!reader.IsDBNull(9)) scholarInfo.AssPeerName = reader.GetString(9);
					if (!reader.IsDBNull(10)) scholarInfo.SchlCate = reader.GetString(10);

					scholarInfo.MarkOld();
					scholarInfolist.Add(scholarInfo);
				}
				reader.Close();
			}
			return scholarInfolist;
		}
		#endregion
	}
}
